﻿using BrightIdeasSoftware;
using ComponentFactory.Krypton.Toolkit;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using TaggingToolkit.Framework;

namespace TaggingToolkit.Controls
{
    public class ColoredTextRendererDataTags : HighlightTextRenderer
    {
        internal const TextFormatFlags NormalTextFormatFlags = TextFormatFlags.NoPrefix |
                                                       TextFormatFlags.EndEllipsis |
                                                       TextFormatFlags.PreserveGraphicsTranslateTransform;

        //private IPalette _palette;

        private bool ColumnIsPrimary
        {
            get { return this.Column != null && this.Column.Index == 0; }
        }

        public ColoredTextRendererDataTags() : base()
        {
            //_palette = KryptonManager.CurrentGlobalPalette;

            //if (_palette != null)
            //    _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            //KryptonManager.GlobalPaletteChanged += new EventHandler(OnGlobalPaletteChanged);
        }

        //private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        //{
        //    ListView?.Invalidate();
        //}

        //private void OnGlobalPaletteChanged(object sender, EventArgs e)
        //{
        //    if (_palette != null)
        //        _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

        //    _palette = KryptonManager.CurrentGlobalPalette;

        //    if (_palette != null)
        //        _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

        //    ListView?.Invalidate();
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    if (disposing)
        //    {
        //        if (_palette != null)
        //        {
        //            _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);
        //            _palette = null;
        //        }

        //        KryptonManager.GlobalPaletteChanged -= new EventHandler(OnGlobalPaletteChanged);
        //    }

        //    base.Dispose(disposing);
        //}

        ///// <summary>
        ///// Return the color of the background color when the item is selected
        ///// </summary>
        ///// <returns>The background color of the subitem</returns>
        //public override Color GetSelectedBackgroundColor()
        //{
        //    if (this.ListView.Focused)
        //        return _palette.GetBackColor1(PaletteBackStyle.ButtonAlternate, PaletteState.Pressed);

        //    if (!this.ListView.HideSelection)
        //        return _palette.GetBackColor1(PaletteBackStyle.ButtonAlternate, PaletteState.Pressed);

        //    return _palette.GetBackColor1(PaletteBackStyle.ButtonAlternate, PaletteState.Pressed);
        //}

        ///// <summary>
        ///// Return the color of the foreground color when the item is selected
        ///// </summary>
        ///// <returns>The foreground color of the subitem</returns>
        //public override Color GetSelectedForegroundColor()
        //{
        //    if (this.ListView.Focused)
        //        return _palette.GetContentShortTextColor1(PaletteContentStyle.ButtonAlternate, PaletteState.Pressed);

        //    if (!this.ListView.HideSelection)
        //        return _palette.GetContentShortTextColor1(PaletteContentStyle.ButtonAlternate, PaletteState.Pressed);

        //    return _palette.GetContentShortTextColor1(PaletteContentStyle.ButtonAlternate, PaletteState.Pressed);
        //}

        /// <summary>
        /// Draw text using GDI
        /// </summary>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <param name="txt"></param>
        protected override void DrawTextGdi(Graphics g, Rectangle r, string txt)
        {
            Color backColor = Color.Transparent;
            if (this.IsDrawBackground && this.IsItemSelected && ColumnIsPrimary && !this.ListView.FullRowSelect)
                backColor = this.GetSelectedBackgroundColor();

            TextFormatFlags flags = NormalTextFormatFlags | this.CellVerticalAlignmentAsTextFormatFlag;

            if (!this.CanWrap)
                flags |= TextFormatFlags.SingleLine;
            var tag = (IHighlightItem)this.ListItem.RowObject;
            TextRenderer.DrawText(g, txt, this.Font, r, tag.HighlightEnabled ? tag.HighlightColor : GetForegroundColor(), backColor, flags);
        }

        /// <summary>
        /// Draw the text using GDI+
        /// </summary>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <param name="txt"></param>
        protected override void DrawTextGdiPlus(Graphics g, Rectangle r, string txt)
        {
            using (StringFormat fmt = this.StringFormatForGdiPlus)
            {
                Font f = this.Font;
                if (this.IsDrawBackground && this.IsItemSelected && this.ColumnIsPrimary && !this.ListView.FullRowSelect)
                {
                    SizeF size = g.MeasureString(txt, f, r.Width, fmt);
                    Rectangle r2 = r;
                    r2.Width = (int)size.Width + 1;
                    using (Brush brush = new SolidBrush(this.GetSelectedBackgroundColor()))
                    {
                        g.FillRectangle(brush, r2);
                    }
                }
                RectangleF rf = r;
                var tag = (IHighlightItem)this.ListItem.RowObject;
                using (Brush brush = new SolidBrush(tag.HighlightEnabled ? tag.HighlightColor : GetForegroundColor()))
                {
                    g.DrawString(txt, f, brush, rf, fmt);
                }
            }
        }

        protected override int DrawImage(Graphics g, Rectangle r, object imageSelector)
        {
            int result = base.DrawImage(g, r, imageSelector);
            if (result == 0)
                return result;

            var tag = (IHighlightItem)this.ListItem.RowObject;
            if (tag.HighlightEnabled)
            {
                using (var pen = new Pen(tag.HighlightColor, 2f))
                {
                    
                    g.DrawRectangle(pen, new Rectangle(Bounds.X, Bounds.Y, result - 2, result - 2));
                }
                
            }

            return result;
        }
    }
}
